﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.Charts;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Visualization.DataModel;

namespace Visualization
{
	/// <summary>
	/// Interaction logic for TrendingGraph.xaml
	/// </summary>
    /// 
	public partial class TrendingGraph : Window
	{
        Random random = new Random();
		public TrendingGraph(System.Windows.Forms.Screen screen)
		{
			this.InitializeComponent();
            this.Show();
            this.WindowState = System.Windows.WindowState.Normal;
            this.Left = screen.Bounds.Left;
            this.Top = screen.Bounds.Top;
            this.WindowState = System.Windows.WindowState.Maximized;
			// Insert code required on object creation below this point.
		}

        Func<Point, Point> mapping = delegate(Point p) { return p; };

        public void createTrend(List<SFPStep> steps, System.IO.FileInfo src)
        {
            String text = "";
            EnumerableDataSource<Point> xSrc = new EnumerableDataSource<Point>(generateStepPoints(steps, GraphVar.X));
            EnumerableDataSource<Point> ySrc = new EnumerableDataSource<Point>(generateStepPoints(steps, GraphVar.Y));
            EnumerableDataSource<Point> thetaSrc = new EnumerableDataSource<Point>(generateStepPoints(steps, GraphVar.Theta));
            xSrc.SetXYMapping(mapping);
            if(src.Name.Contains("Combine"))
                text = "Bayes Solution";
            else
                text = "Non-Bayes Solution";
            xTrend.AddLineGraph(xSrc,text);
            ySrc.SetXYMapping(mapping);
            yTrend.AddLineGraph(ySrc,text);
            thetaSrc.SetXYMapping(mapping);
            thetaTrend.AddLineGraph(thetaSrc,text);
        }

        private System.Windows.Media.Brush GetRandomBrush() {
            SolidColorBrush brush = new SolidColorBrush();
            double r, g, b;
            double lightness = random.NextDouble() * 0.5 + 0.4; // not too dark nor too light
            double hue = random.NextDouble() * 360.0; // full hue spectrum
            double saturation = random.NextDouble() * 0.8 + 0.2; // not too grayish
            ColorTransform transform = new ColorTransform();
            transform.HSLtoRGB(hue, saturation, lightness, out r, out g, out b);
            brush.Color = System.Windows.Media.Color.FromRgb((byte)(r * 255.0), (byte)(g * 255.0), (byte)(b * 255.0));
            return brush;
        }

        private System.Collections.IEnumerable generateStepPoints(List<SFPStep> steps, GraphVar var)
        {
            List<Point> pts = new List<Point>();
            foreach (SFPStep step in steps)
            {
                if(var.Equals(GraphVar.X))
                    pts.Add(new Point(step.Step, step.X));
                else if (var.Equals(GraphVar.Y))
                    pts.Add(new Point(step.Step, step.Y));
                else
                    pts.Add(new Point(step.Step, step.Theta));
            }
            return pts;
        }

        private System.Collections.IEnumerable generateParticlePoints(List<SFPStep> steps, GraphVar var)
        {
            List<Point> pts = new List<Point>();
            foreach (SFPStep step in steps)
            {
                if (var.Equals(GraphVar.X))
                    pts.Add(new Point(step.Step, step.HighestProbabilityParticle.X));
                else if (var.Equals(GraphVar.Y))
                    pts.Add(new Point(step.Step, step.HighestProbabilityParticle.Y));
                else
                    pts.Add(new Point(step.Step, step.HighestProbabilityParticle.Theta));
            }
            return pts;
        }

        enum GraphVar : sbyte
        {
            X = 1,
            Y = 2,
            Theta = 3
        }
	}
}